<?php

namespace Database\Seeders;

use App\Models\Order;
use App\Models\Room;
use App\Models\Dish;
use App\Models\User;
use App\Models\RoomMember;
use Illuminate\Database\Seeder;
use Carbon\Carbon;

class OrderSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 获取所有房间和菜品
        $rooms = Room::with('members')->get();
        $dishes = Dish::where('is_available', true)->get();
        
        if ($rooms->isEmpty() || $dishes->isEmpty()) {
            $this->command->warn('没有房间或菜品数据，请先运行相关Seeder');
            return;
        }
        
        // 订单状态
        $orderStatuses = ['pending', 'confirmed', 'cancelled', 'completed'];
        
        // 口味选项
        $flavors = [
            '微辣', '中辣', '特辣', '不辣', '酸甜', '咸鲜', '清淡', '浓郁',
            '香辣', '麻辣', '酸辣', '甜辣', '五香', '蒜香', '葱香', '姜香'
        ];
        
        // 备注模板
        $notes = [
            '不要辣', '少盐', '多放菜', '少油', '不要香菜', '不要葱', '不要蒜',
            '微辣即可', '中辣', '特辣', '不要味精', '少糖', '多放汤', '少汤',
            '打包', '堂食', '外卖', '加急', '正常速度', '慢一点', '快一点',
            '老人吃', '小孩吃', '孕妇吃', '病人吃', '减肥餐', '营养餐', '清淡点',
            '浓郁点', '香味重点', '香味淡点', '口感软点', '口感硬点', '温度热点',
            '温度凉点', '新鲜点', '熟透点', '嫩点', '老点', '脆点', '软点'
        ];
        
        $orderCount = 0;
        
        // 为每个房间创建订单
        foreach ($rooms as $room) {
            // 获取房间成员
            $members = $room->members;
            
            if ($members->isEmpty()) {
                continue;
            }
            
            // 每个房间创建1-10个订单
            $roomOrderCount = rand(1, 10);
            
            for ($i = 0; $i < $roomOrderCount; $i++) {
                $user = $members->random();
                
                // 随机选择1-5个菜品（不超过可用菜品数量）
                $maxDishes = min(5, $dishes->count());
                $dishCount = rand(1, $maxDishes);
                $selectedDishes = $dishes->random($dishCount);
                
                $orderDishes = [];
                
                foreach ($selectedDishes as $dish) {
                    $quantity = rand(1, 3);
                    
                    $orderDishes[] = [
                        'dish_id' => $dish->id,
                        'name' => $dish->name,
                        'quantity' => $quantity,
                        'flavor' => rand(1, 10) <= 6 ? $flavors[array_rand($flavors)] : null, // 60%概率有口味要求
                        'note' => rand(1, 10) <= 3 ? $notes[array_rand($notes)] : null, // 30%概率有备注
                    ];
                }
                
                
                // 生成订单时间
                $orderTime = $room->created_at->addMinutes(rand(1, 1440)); // 房间创建后1分钟内到24小时内
                
                // 创建订单
                $order = Order::create([
                    'order_id' => 'order_' . time() . '_' . rand(10000, 99999) . '_' . $i,
                    'room_id' => $room->id,
                    'user_id' => $user->id,
                    'dishes' => $orderDishes,
                    'status' => $orderStatuses[array_rand($orderStatuses)],
                    'notes' => rand(1, 10) <= 2 ? $notes[array_rand($notes)] : null, // 20%概率有订单备注
                    'created_at' => $orderTime,
                    'updated_at' => $orderTime->addMinutes(rand(1, 120)),
                ]);
                
                $orderCount++;
                
                // 更新菜品的点餐次数
                foreach ($selectedDishes as $dish) {
                    $dish->increment('order_count', $quantity);
                }
            }
            
            // 更新房间统计
            $roomOrders = Order::where('room_id', $room->id)->get();
            $room->update([
                'total_orders' => $roomOrders->count(),
            ]);
        }
        
        $this->command->info("已创建 {$orderCount} 个订单数据");
    }
}

// 为Order模型添加generateOrderId方法
if (!method_exists(Order::class, 'generateOrderId')) {
    Order::macro('generateOrderId', function () {
        return 'order_' . time() . '_' . rand(1000, 9999);
    });
}
